//
//  NewManger.m
//  FiveWork
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 Project. All rights reserved.
//

#import "NewManger.h"
#import "NewModel.h"
#define NewsURL @ "http://c.3g.163.com/nc/article/headline/T1348647909107/0-140.html"
#define NewssURL @"http://c.m.163.com/nc/article/headline/T1348647853363/160-20.html"
@interface NewManger ()



@end

@implementation NewManger

static NewManger *newManger =nil;
+(instancetype)defaultManger{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        newManger =[[NewManger alloc] init];
        
    });
    return newManger;
}

/**
 *  懒加载
 */
-(NSMutableArray *)pressArray{
    if (_pressArray ==nil) {
        _pressArray =[NSMutableArray array];
        
    }
    return _pressArray;
}



/**
 *  解析数据
 *
 *
 */


-(void)requestDataWithHandle:(void (^)())handle uporDown:(BOOL)upDown{
    NSURL *url =[NSURL URLWithString:NewsURL];
NSURLSessionDataTask *dataTask =[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            if (error || !data) {
                return ;
            }
            if (upDown ==YES) {
               
    
            }else{
                [self.pressArray removeAllObjects];
    
            }
    
            NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray *tList =[NSArray array];
            tList =dic[@"T1348647909107"];
            for (NSDictionary *dic in tList) {
                NewModel *model =[NewModel new];
                [model setValuesForKeysWithDictionary:dic];
                [self.pressArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                handle();
            });
            
        }];
        
        
        [dataTask resume];
    

    
    
}

/**
 *  解析数据
 */
/*
-(void)requestDataWithHandle:(void(^)())handle uporDown:(BOOL)upDown{
    NSURL *url =[NSURL URLWithString:NewsURL];
    NSURL *url2 = [NSURL URLWithString:NewssURL];
    
    if (upDown == YES) {
        
        [self displayDataWithUrl:url withBlock:handle upDown:upDown];
    } else if(upDown ==NO){
          [self.pressArray removeAllObjects];
        [self displayDataWithUrl:url2 withBlock:handle upDown:upDown];
    }
    
    
//    
//    NSURLSessionDataTask *dataTask =[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//        if (error || !data) {
//            return ;
//        }
//        if (upDown ==YES) {
//            [self displayDataWithUrl:nil withBlock:handle];
//            
//        }else{
//            [self.pressArray removeAllObjects];
//
//        }
//     
//        NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        NSArray *tList =[NSArray array];
//        tList =dic[@"T1348647909107"];
//        for (NSDictionary *dic in tList) {
//            NewModel *model =[NewModel new];
//            [model setValuesForKeysWithDictionary:dic];
//            [self.pressArray addObject:model];
//        }
//        dispatch_async(dispatch_get_main_queue(), ^{
//            handle();
//        });
//        
//    }];
//    
//    
//    [dataTask resume];
    
}


- (void)displayDataWithUrl:(NSURL *)url1  withBlock:(void (^)())handle upDown:(BOOL)upDown
{

//    NSURL *url =[NSURL URLWithString:NewssURL];
    NSURLSessionDataTask *dataTask =[[NSURLSession sharedSession] dataTaskWithURL:url1 completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error || !data) {
            return ;
        }
        
        NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *tList =[NSArray array];
        if (upDown == YES) {
            
            tList =dic[@"T1348647909107"];
        }else {
        
            tList =dic[@"T1348647853363"];
        }
       for (NSDictionary *dic in tList) {
            NewModel *model =[NewModel new];
          
            [model setValuesForKeysWithDictionary:dic];
            
            [self.pressArray addObject:model];
           
            
        }
       
       
        dispatch_async(dispatch_get_main_queue(), ^{
            handle();
        });
        
    }];
 
    
    [dataTask resume];

//    for (NewModel *mod in self.pressArray) {
//        if (mod.imgextra != nil) {
//            [self.pressArray removeObject:mod];
//        }
//    }
}
*/

//-(void)displayDataWithHandle:(void(^)())Handle upOrDown:(BOOL)upDown{
//    NSURL *url =[NSURL URLWithString:NewssURL];
//    NSURLSessionDataTask *dataTask =[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//        if (error) {
//            return ;
//        }
//        if (upDown ==YES) {
//            
//        }else{
//            [self.pressArray removeAllObjects];
//            
//        }
//   
//        NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        NSArray *tList =[NSArray array];
//        tList =dic[@"T1348647853363"];
//        for (NSDictionary *dic in tList) {
//            NewModel *model =[NewModel new];
//            [model setValuesForKeysWithDictionary:dic];
//            [self.pressArray addObject:model];
//        }
//        dispatch_async(dispatch_get_main_queue(), ^{
//            Handle();
//        });
//        
//    }];
//    
//    
//    [dataTask resume];
//}
//
//















/**
 *  返回NewArray
 */
-(NSUInteger)NewArrayCount{
    
    return self.pressArray.count;
    
}


@end
